<template>
  <div id="List">
    <!-- 面包屑导航区 -->
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item :to="{ path: '/home' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>商品管理</el-breadcrumb-item>
      <el-breadcrumb-item>商品列表</el-breadcrumb-item>
    </el-breadcrumb>
    <!-- 卡片视图区域 -->
    <el-card>
      <el-row :gutter="20">
        <el-col :span="8">
          <el-input placeholder="请输入内容" v-model="queryInfo.query" :clearable="true" @clear="getGoodsList"> 
            <el-button slot="append" icon="el-icon-search" @click="getGoodsList"></el-button>
          </el-input>
        </el-col>
        <el-col :span="4">
          <el-button type="primary" @click="goAddpage">添加商品</el-button>
        </el-col>
      </el-row>
      <!-- Table表格区域 -->
      <el-table :data="goodslist" :border="true" :stripe="true">
        <el-table-column type="index"></el-table-column>
        <el-table-column label="商品名称" prop="goods_name"></el-table-column>
        <el-table-column width="95px" label="商品价格(元)" prop="goods_price"></el-table-column>
        <el-table-column width="70px" label="商品重量" prop="goods_weight"></el-table-column>
        <el-table-column width="140px" label="创建时间" prop="add_time">
          <template slot-scope="scope">
            <!-- 调用在main里面注册的全局过滤器dateFormat -->
            {{scope.row.add_time | dateFormat}}
          </template>
        </el-table-column>
        <el-table-column width="130px" label="操作">
          <template slot-scope="scope">
            <el-button type="primary" icon="el-icon-edit" size="mini"></el-button>
            <el-button @click="removeById(scope.row)" type="danger" icon="el-icon-delete" size="mini"></el-button>
          </template>
        </el-table-column>
      </el-table>
      <!-- 分页 -->
      <el-pagination
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
      :current-page="queryInfo.pagenum"
      :page-sizes="[5, 10, 15, 20]"
      :page-size="queryInfo.pagesize"
      layout="total, sizes, prev, pager, next, jumper"
      :total="total">
      </el-pagination>
    </el-card>
  </div>
</template>

<script>
import { getHttpGoodsList, deleteHttpDeleteGoodsById  } from 'network/goodslist'

export default {
  name: "List",
  data() {
    return {
      //查询参数
      queryInfo: {
        query: '',
        pagenum: 1,
        pagesize: 5
      },
      //总数据条数
      total: 0,
      //商品列表
      goodslist: []
    }
  },
  created() {
    this.getGoodsList()
  },
  methods: {
    //根据分页获取对应的商品列表
    getGoodsList() {
      getHttpGoodsList(this.queryInfo).then(res => {
        if( res.meta.status !== 200) {         
          this.$message.error(res.meta.msg)
          return
        }
        else {
          this.total = res.data.total
          this.goodslist = res.data.goods
        }
      }).catch(err => {
        this.$message.error(err)
      })
    },
    //分页控件调整分页页值大小的事件
    handleSizeChange(newSize) {
      this.queryInfo.pagesize = newSize
      this.getGoodsList()
    },
    //分页控件页码发生变化的事件
    handleCurrentChange(newPageNum) {
      this.queryInfo.pagenum = newPageNum
      this.getGoodsList()
    },
    //删除商品单击事件
    removeById(row) {
      //弹窗询问用户是否删除数据
      this.$confirm('此操作将永久删除该参数, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        deleteHttpDeleteGoodsById(row.goods_id).then(res => {
          if( res.meta.status !== 200) {
            this.$message.error(res.meta.msg)
            return
          }
          else {
            this.$message.success(res.meta.msg)
            this.getGoodsList()
          }
        }).catch(err => {
          this.$message.error(err)
        })
        
      }).catch(() => {
        this.$message({ type: 'info',message: '已取消删除' })          
      })
    },
    //添加商品事件
    goAddpage() {
      this.$router.push('/goods/add')
    }
  }
}
</script>

<style scoped>
</style>
